
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.text.BadLocationException;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author tekoraik
 */
public class VistaTexto extends JPanel {
    private CITexto cit;
    private JTabbedPane jtp;
    private CITexto vcit;
    private SpringLayout layout;
    private ArrayList<JTextArea> textos;
    private ActionTancarPestana a;
    public VistaTexto(CITexto c){
        super();
        cit=c;
        inicializar();
    }
    public void visualizar(){
        this.setVisible(true);
    }
    
    public JTabbedPane getTabbedPane() {
        return jtp;
    }

    public void nuevoTexto(){
           
           JTextArea textarea = new JTextArea();
           JScrollPane scroll = new JScrollPane(textarea);
           jtp.addTab("Nuevo Texto", scroll);
           jtp.setTabComponentAt(jtp.getTabCount()-1, new ButtonTabComponent(jtp,a));
           textos.add(textarea);
           jtp.setSelectedIndex(jtp.getTabCount()-1);
           textarea.grabFocus();
    }
    public void subrayarPalabra(int offset,String pal, JTextArea area) {
        area.requestFocus();
        if (area.getText().length()>=offset){
            int pos = area.getText().indexOf(pal, offset);
            if (pos>=0){
                area.setSelectionStart(pos);
                area.setSelectionEnd(pos+pal.length());
            }
        }
    }

    public void seleccionarTodo() {
        textos.get(jtp.getSelectedIndex()).requestFocus();
        textos.get(jtp.getSelectedIndex()).selectAll();
    }

    public void copiarTexto() {
        textos.get(jtp.getSelectedIndex()).copy();
    }

    public void borrarTexto() {
        textos.get(jtp.getSelectedIndex()).removeAll();
    }

    public void settexto(ArrayList<String> lineas) {
        borrarTexto();
        textos.get(jtp.getSelectedIndex()).setText("");
        for (int i=0; i<lineas.size(); i++){
            textos.get(jtp.getSelectedIndex()).append(lineas.get(i));
            textos.get(jtp.getSelectedIndex()).append("\n");
        }
    }
    private boolean escaracter(char a) {
        if (a == '.' || a == ',' || a == ' ' || a == '?' || a == '!' || a == ':' || a == ';' || a == '\n') return true;
        return false;
    }
    public void subrayar(String s, int rep) {
        String aux = textos.get(jtp.getSelectedIndex()).getText();
        int i = 0;
        int j = 0;
        int repetir = 1;
        boolean b = false;
        while(i < aux.length() && b == false) {
            String aux2 = "";
            j = i;
            while (i < aux.length() && !escaracter(aux.charAt(i)) && b == false) {
                 ++i;
            }
            if(i - j > 0) {
                aux2 = aux.substring(j, i);
                if(aux2.equals(s)) {
                    if(rep == repetir) b = true;
                    else ++repetir;
                }
            }
            ++i;
        }
        this.subrayarPalabra(j, s, textos.get(jtp.getSelectedIndex()));
    }

    public void actualizarLongitud() {

        ArrayList<String> act = obtenerLineas();
        int i;
        for(i = 0; i < act.size(); ++i) {
            if(act.get(i).length() > 135) {
                int j = 115;
                boolean b = false;
                while (b == false && j < act.get(i).length()) {
                    if(act.get(i).charAt(j) == ' ') b = true;
                    else ++j;
                }
                if(i + 1 < act.size()) {
                    String s = " ".concat(act.get(i+1));
                    String s1 = act.get(i).substring(j + 1, act.get(i).length()).concat(s);
                    act.set(i + 1, s1);
                }
                else act.add(i + 1, act.get(i).substring(j + 1, act.get(i).length()));
                act.set(i, act.get(i).substring(0, j));
            }
        }
        settexto(act);  
        cit.actualizartexto();
    }

    public void pegarTexto() {
        textos.get(jtp.getSelectedIndex()).paste();
        actualizarLongitud();
    }
    
    public void cortarTexto() {
        textos.get(jtp.getSelectedIndex()).cut();
    }
    
    public void cerrarTexto() {
        if (jtp.getComponentCount() > 1) jtp.remove(jtp.getSelectedIndex());
    }
    
    public void mostrarError(String textoError,Component cent){
        JOptionPane.showMessageDialog(cent, textoError, "Error", JOptionPane.ERROR_MESSAGE);
    }
    public String preguntarRuta(Component cent,String tipo, String titulo){
        String ruta = "";
        int result;
        JFileChooser jfc = new JFileChooser();
        jfc.setApproveButtonText(tipo);
        jfc.setDialogType(JFileChooser.OPEN_DIALOG);
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.setDialogTitle(titulo);
        result = jfc.showOpenDialog(cent);
        if (result == JFileChooser.APPROVE_OPTION){
            ruta = jfc.getSelectedFile().getPath();
        }
        return ruta;
    }

    public ArrayList<String> obtenerLineas(){
        ArrayList<String> result = new ArrayList<String>();
        String[] lineas = textos.get(jtp.getSelectedIndex()).getText().split("\n");
        for (int i=0; i<lineas.length; i++){
            result.add(lineas[i]);
        }
        return result;
    }
    public void cambiarTituloPestana(String titulo){
        jtp.setTitleAt(jtp.getSelectedIndex(), titulo);
    }
    public void anadirTexto(ArrayList<String> lineas, String titulo){
        JTextArea act = new JTextArea();
        JScrollPane scroll = new JScrollPane(act);
        jtp.addTab(titulo, scroll);
        jtp.setTabComponentAt(jtp.getTabCount()-1, new ButtonTabComponent(jtp,a));
        jtp.setSelectedIndex(jtp.getTabCount()-1);
        act.setText("");
        for (int i=0; i<lineas.size(); i++){
            act.append(lineas.get(i));
            act.append("\n");
        }
        textos.add(act);

    }
    private boolean tancaPest(int i){
        textos.remove(i);
        return true;
    }
    private void inicializar() {
        jtp = new JTabbedPane();
        layout = new SpringLayout();
        textos = new ArrayList<JTextArea>();
        a = new ActionTancarPestana(){

            public boolean run(int i) {
               return tancaPest(i);
            }
        };
        inicializarPanel();
        inicializarPestanas();
        
        inicializarLayout();
        montar();
    }
    private void inicializarPanel(){
        this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Texto"));
    }
    private void inicializarLayout(){
        this.setLayout(layout);
        layout.putConstraint(SpringLayout.NORTH,jtp, 0,SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.SOUTH,jtp, 0,SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.EAST,jtp, 0,SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST,jtp, 0,SpringLayout.WEST, this);
    }
    private void inicializarPestanas(){
        nuevoTexto();

    }
    private void montar(){
        this.add(jtp);
    }

}
